home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Python 1.1 / Python / pythonmain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-29  |  5.6 KB  |  205 lines  |  [TEXT/KAHL]

  1. /***********************************************************
  2. Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
  3. Amsterdam, The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /* Python interpreter main program */
  26.  
  27. #include "allobjects.h"
  28.  
  29. extern int debugging; /* Needed in parser.c, declared in pythonrun.c */
  30. extern int verbose; /* Needed in import.c, declared in pythonrun.c */
  31. extern int suppress_print; /* Needed in ceval.c, declared in pythonrun.c */
  32.  
  33. /* Interface to getopt(): */
  34. extern int optind;
  35. extern char *optarg;
  36. extern int getopt(); /* PROTO((int, char **, char *)); -- not standardized */
  37.  
  38. extern char *getenv();
  39.  
  40. extern char *getversion();
  41. extern char *getcopyright();
  42.  
  43. int
  44. realmain(argc, argv)
  45.     int argc;
  46.     char **argv;
  47. {
  48.     int c;
  49.     int sts;
  50.     char *command = NULL;
  51.     char *filename = NULL;
  52.     FILE *fp = stdin;
  53.     char *p;
  54.     int inspect = 0;
  55.     int unbuffered = 0;
  56.  
  57.     if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
  58.         debugging = 1;
  59.     if ((p = getenv("PYTHONSUPPRESS")) && *p != '\0')
  60.         suppress_print = 1;
  61.     if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
  62.         verbose = 1;
  63.     if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
  64.         inspect = 1;
  65.     if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
  66.         unbuffered = 1;
  67.  
  68.     while ((c = getopt(argc, argv, "c:disuv")) != EOF) {
  69.         if (c == 'c') {
  70.             /* -c is the last option; following arguments
  71.                that look like options are left for the
  72.                the command to interpret. */
  73.             command = malloc(strlen(optarg) + 2);
  74.             if (command == NULL)
  75.                 fatal("not enough memory to copy -c argument");
  76.             strcpy(command, optarg);
  77.             strcat(command, "\n");
  78.             break;
  79.         }
  80.         
  81.         switch (c) {
  82.  
  83.         case 'd':
  84.             debugging++;
  85.             break;
  86.  
  87.         case 'i':
  88.             inspect++;
  89.             break;
  90.  
  91.         case 's':
  92.             suppress_print++;
  93.             break;
  94.  
  95.         case 'u':
  96.             unbuffered++;
  97.             break;
  98.  
  99.         case 'v':
  100.             verbose++;
  101.             break;
  102.  
  103.         /* This space reserved for other options */
  104.  
  105.         default:
  106.             fprintf(stderr,
  107. "usage: %s [-d] [-i] [-s] [-u ] [-v] [-c cmd | file | -] [arg] ...\n",
  108.                 argv[0]);
  109.             fprintf(stderr, "\
  110. \n\
  111. Options and arguments (and corresponding environment variables):\n\
  112. -d     : debug output from parser (also PYTHONDEBUG=x)\n\
  113. -i     : inspect interactively after running script (also PYTHONINSPECT=x)\n\
  114. -s     : suppress the printing of top level expressions (also PYTHONSUPPRESS=x)\n\
  115. -u     : unbuffered stdout and stderr (also PYTHONUNBUFFERED=x)\n\
  116. -v     : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
  117. -c cmd : program passed in as string (terminates option list)\n\
  118. ");
  119.             /* ANSI does not allow strings > 512 chars
  120.                and MPW doesn't like it either -- so split it! */
  121.             fprintf(stderr, "\
  122. file   : program read from script file\n\
  123. -      : program read from stdin (default; interactive mode if a tty)\n\
  124. arg ...: arguments passed to program in sys.argv[1:]\n\
  125. \n\
  126. Other environment variables:\n\
  127. PYTHONSTARTUP: file executed on interactive startup (no default)\n\
  128. PYTHONPATH   : colon-separated list of directories prefixed to the\n\
  129.                default module search path.  The result is sys.path.\n\
  130. ");
  131.             exit(2);
  132.             /*NOTREACHED*/
  133.  
  134.         }
  135.     }
  136.  
  137.     if (unbuffered) {
  138. #ifndef MPW
  139.         setbuf(stdout, (char *)NULL);
  140.         setbuf(stderr, (char *)NULL);
  141. #else
  142.         /* On MPW (3.2) unbuffered seems to hang */
  143.         setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
  144.         setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
  145. #endif
  146.     }
  147.  
  148.     if (command == NULL && optind < argc && strcmp(argv[optind], "-") != 0)
  149.         filename = argv[optind];
  150.  
  151.     if (verbose ||
  152.         command == NULL && filename == NULL && isatty((int)fileno(fp)))
  153.         fprintf(stderr, "Python %s\n%s\n",
  154.             getversion(), getcopyright());
  155.     
  156.     if (filename != NULL) {
  157.         if ((fp = fopen(filename, "r")) == NULL) {
  158.             fprintf(stderr, "%s: can't open file '%s'\n",
  159.                 argv[0], filename);
  160.             exit(2);
  161.         }
  162.     }
  163.     
  164.     initall();
  165.     
  166.     if (command != NULL) {
  167.         /* Backup optind and force sys.argv[0] = '-c' */
  168.         optind--;
  169.         argv[optind] = "-c";
  170.     }
  171.  
  172.     setpythonargv(argc-optind, argv+optind);
  173.  
  174.     if (command) {
  175.         sts = run_command(command) != 0;
  176.     }
  177.     else {
  178.         if (filename == NULL && isatty((int)fileno(fp))) {
  179.             char *startup = getenv("PYTHONSTARTUP");
  180. #ifdef macintosh
  181.             if (startup == NULL)
  182.                 startup = "PythonStartup";
  183. #endif
  184.             if (startup != NULL && startup[0] != '\0') {
  185.                 FILE *fp = fopen(startup, "r");
  186.                 if (fp != NULL) {
  187.                     (void) run_script(fp, startup);
  188.                     err_clear();
  189.                     fclose(fp);
  190.                 }
  191.             }
  192.         }
  193.         sts = run(fp, filename == NULL ? "<stdin>" : filename) != 0;
  194.         if (filename != NULL)
  195.             fclose(fp);
  196.     }
  197.  
  198.     if (inspect && isatty((int)fileno(stdin)) &&
  199.         (filename != NULL || command != NULL))
  200.         sts = run(stdin, "<stdin>") != 0;
  201.  
  202.     goaway(sts);
  203.     /*NOTREACHED*/
  204. }
  205.